#!/usr/bin/env python

import os
import thread
from threading import Thread
from socket import *

class RequestHandler(Thread):

	def __init__ (self, connection):
		Thread.__init__(self)
		self.connection = connection
	
	def run(self):
		while 1:
			data = self.connection.recv(1024) #max length of an url :2083
			if data:
				requests = data.rstrip().split(";")
				for i in range(0,len(requests)):
					req = requests[i]
					urls = req.rstrip().split(",")
                			if(len(urls) == 2):
						"""
						check in Mysql if url has been requested in the timestamp
						if so => send ID over socket
						if not => render image & send ID over socket
						""" 
						thread.start_new_thread(self.execRenderer, (urls[0], urls[1]))
			else:
				self.connection.close()
				break

	def execRenderer(self, weburl, imgurl):
		os.system(("python /home/fries/Documents/screendaemon/WebkitRenderer.py %s %s") % (weburl, imgurl))
